package com.x.erp.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.x.erp.model.Car;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.x.erp.model.CarConf;
import com.x.framework.dao.BaseDao;
@Repository
public class CarConfDao extends BaseDao {

	private final static String selectConfSql = "SELECT " + 
			"bcc.* " + 
			"FROM " + 
			"BASE_CAR_CONF bcc " +
			"WHERE 1 = 1 ";
	/**
	 * 查询车辆配置信息
	 * @param carConf
	 * @return
	 * @throws Exception 
	 */
	public List<Map<String, Object>> selectCarConf(CarConf carConf) throws Exception {
		StringBuilder sql = new StringBuilder(selectConfSql);
		List<Object> params = new ArrayList<>();
		searchCondition(sql , params, carConf);
		sql.append("ORDER BY bcc.CONF_SEQ ");
		RowMapper<Map<String, Object>> mapper = new RowMapper<Map<String, Object>>() {
			public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("confId", rs.getInt("CONF_ID"));
				map.put("confName", rs.getString("CONF_NAME"));
				map.put("confSeq", rs.getInt("CONF_SEQ"));
				map.put("confKey", rs.getString("CONF_KEY"));
				map.put("confParentId", rs.getInt("CONF_PARENT_ID"));
				map.put("confBrightSpot", rs.getInt("CONF_BRIGHT_SPOT"));
				map.put("selectedState", false);
				return map;
			}
		};
		return this.queryForList(sql.toString(), mapper, params);
	}

	// 拼接查询条件
	private void searchCondition(StringBuilder sql, List<Object> params, CarConf carConf) {
		if (carConf != null) {
			if (carConf.getConfId() != null) {
				sql.append("AND bcc.CONF_ID = ? ");
				params.add(carConf.getConfId());
			}
			if (carConf.getConfParentId() != null) {
				sql.append("AND bcc.CONF_PARENT_ID = ? ");
				params.add(carConf.getConfParentId());
			}
			if (carConf.getConfBrightSpot() != null) {
				sql.append("AND bcc.CONF_BRIGHT_SPOT = ? ");
				params.add(carConf.getConfBrightSpot());
			}
		}
	}
}
